package com.xizi.forum.controller;

import com.xizi.forum.common.AppResult;
import com.xizi.forum.common.Constants;
import com.xizi.forum.common.ResultCode;
import com.xizi.forum.exception.ApplicationException;
import com.xizi.forum.model.ArticleReplyInfo;
import com.xizi.forum.model.UserInfo;
import com.xizi.forum.service.IArticleReplyInfoService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


// 回复模块
@RestController
@RequestMapping("/reply")
@Slf4j
public class ArticleReplyInfoController {
    @Autowired
    private IArticleReplyInfoService articleReplyInfoService;

    /**
     * 新建回复帖子
     * @param request
     * @param articleId 帖子作者id
     * @param content 回复帖子内容
     * @return
     */
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request, @NonNull Long articleId, @NonNull String content){
        //1.检查用户是否登入
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constants.USERINFO) == null){
            log.error(ResultCode.FAILED_NOT_LOGIN.toString());
            throw new ApplicationException(ResultCode.FAILED_NOT_LOGIN);
        }
        //2.检查发送帖子的用户状态（如是否被禁言）
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.USERINFO);
        if(userInfo.getState() == Constants.USER_STATE_IS_NOT_SEND_MESSAGE){
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_USER_BANNED);
        }
        // 进行回复帖子创建
        Boolean check = articleReplyInfoService.create(articleId,content,userInfo.getId());

        return AppResult.success();
    }

    /**
     * 获取当前帖子下的所用回复帖子
     * @param articleId 当前帖子作者id
     * @return
     */
    @GetMapping("/getReplies")
    public AppResult getReplies(Long articleId){
        //1.进行参数校验
        if(articleId == null || articleId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        List<ArticleReplyInfo> list =  articleReplyInfoService.selectAllRepliesByArticleId(articleId);
        return AppResult.success(list);
    }

    /**
     * 删除回复帖子（普通用户只能删除自己的）
     * @param request
     * @param articleReplyId 回复帖子id
     * @param articleId 帖子id
     * @return
     */
    @PostMapping("/deleteArticleReplyById")
    public AppResult deleteArticleReplyById(HttpServletRequest request,Long articleReplyId,Long articleId){
        log.info("articleReplyId = " + articleReplyId + ",articleId = " + articleId);
        // 1.参数校验
        if(articleReplyId == null || articleReplyId <= 0 || articleId == null || articleId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        HttpSession session = request.getSession(false);
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USERINFO);

        //2.校验回复帖子的发送者是不是当前登入用户
        ArticleReplyInfo articleReplyInfo = articleReplyInfoService.selectArticleReplyById(articleReplyId);
        if(articleReplyInfo == null || articleReplyInfo.getState() == 1){
            log.error(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS);
        }
        if(articleReplyInfo.getPostUserId() != userInfo.getId()){
            log.error(ResultCode.FAILED_UNAUTHORIZED_ARTICLE_REPLY +
                    "回复用户 id : " + articleReplyInfo.getPostUserId() +
                    "当前登入用户 id" + userInfo.getId());
            throw new ApplicationException(ResultCode.FAILED_UNAUTHORIZED_ARTICLE_REPLY);
        }

        //3.进行删除
        articleReplyInfoService.deleteArticleReplyById(articleReplyId,articleId);
        return AppResult.success();
    }

}
